﻿using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System;
using ShanjinPDAUI.ModelEntity;
using System.Text.Json;
using System.Text;
using ShanjinPDAUI.Commom;
using System.Reflection.PortableExecutable;
using ShanjinPDAUI.Pages;
using System.Linq;
using System.Transactions;

namespace ShanjinPDAUI.Data
{
    public class PickingServer
    {
        public async Task<IEnumerable<Machine_Entity>> GetMachine()
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/Machine/GetMachine");
                    request.Headers.Add("Accept", "text / plain");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<Machine_Entity>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return null;
                    }
                }
            }



        }


        public async Task<IEnumerable<UnpackingAre_Entity>> GetuNPK()
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/Machine/GetReturnGetUnpaks");
                    request.Headers.Add("Accept", "text / plain");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<UnpackingAre_Entity>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return null;
                    }
                }
            }



        }

        public async Task<IEnumerable<FullCache_Entity>> GetCaches(int devid)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/Machine/GetChaches?devid={devid}");
                    request.Headers.Add("Accept", "text / plain");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<FullCache_Entity>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return null;
                    }
                }
            }



        }

        public async Task<IEnumerable<FullCache_Entity>> GetReturnGetChaches()
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/Machine/GetReturnGetChaches");
                    request.Headers.Add("Accept", "text / plain");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<FullCache_Entity>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return null;
                    }
                }
            }



        }


        public async Task<IEnumerable<AGV_Entity>> GetAGVs()
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/Machine/GetAGVs");
                    request.Headers.Add("Accept", "text / plain");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<AGV_Entity>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return null;
                    }
                }
            }



        }

        public async Task<string> EmptyPICK(Machine_Entity machine_Entity, bool Flag,int emptyCahceID=0)
        {

            OutboundPar PpckingPar = new OutboundPar()
            {
                TASK_CODE = "kongjuan",
                TASK_NO = String.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now) + "C" + "PDA",
                // DevId = machine.Devid,

                TO_POSITION = machine_Entity.MESName.Split(",").First(),
                OUT_TYPE = "1",
                LOAD_YN = "1",
                INPUT_POSITION = emptyCahceID.ToString()
            };
            switch (machine_Entity.LkPostation)
            {
                case 1:
                    PpckingPar.FROM_POSITION = "K3TAGR101_1";
                    break;
                case 2:
                    PpckingPar.FROM_POSITION = "K3TAGR101_2";
                    break;
                case 3:
                    PpckingPar.FROM_POSITION = "K3TAGR101_3";
                    break;
                case 4:
                    PpckingPar.FROM_POSITION = "K3TAGR101_4";
                    break;
                case 5:
                    PpckingPar.FROM_POSITION = "K3TAGR102_1";
                    break;
                default:
                    break;
            }
            if (Flag)
            {
                PpckingPar.TASK_SIZE = "11";
            }
            else
            {
                PpckingPar.TASK_SIZE = "8";
            }
            var jsonPar = JsonSerializer.Serialize<OutboundPar>(PpckingPar);
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/Outbound");
                    request.Headers.Add("Accept", "text / plain");
                    request.Content = new StringContent(jsonPar, Encoding.UTF8, "application/json");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            //using var responseStream = await response.Content.ReadAsStreamAsync();
                            //var result = await JsonSerializer.DeserializeAsync
                            //    <string>(responseStream);
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return "调用接口失败";
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return "调用接口异常";
                    }
                }
            }
        }
        public async Task<string> Inbound(Machine_Entity machine_Entity, bool Flag,bool IsDirectOutBuond)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/Inbound");
                    request.Headers.Add("Accept", "text / plain");

                    InboundPar PpckingPar = new InboundPar()
                    {
                        //TASK_NO = DateTime.Now.ToFileTime().ToString() + "PDA",
                        TASK_NO = String.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now) + "PDA",
                        // DevId = machine.Devid,
                        FROM_POSITION = machine_Entity.MESName,
                        DOWN_YN = "1",
                    };
                    switch (machine_Entity.LkPostation)
                    {
                        case 1:
                            PpckingPar.TO_POSITION = "K3TAGR101_1";
                            break;
                        case 2:
                            PpckingPar.TO_POSITION = "K3TAGR101_2";
                            break;
                        case 3:
                            PpckingPar.TO_POSITION = "K3TAGR101_3";
                            break;
                        case 4:
                            PpckingPar.TO_POSITION = "K3TAGR101_4";
                            break;
                        case  5:
                            PpckingPar.TO_POSITION = "K3TAGR102_1";
                            break;
                        default:
                            break;
                    }
                    if (Flag)
                    {
                        PpckingPar.TASK_Diam = "11";
                    }
                    else
                    {
                        PpckingPar.TASK_Diam = "8";
                    }
                    if(IsDirectOutBuond)
                    {
                        PpckingPar.TO_POSITION = "MACH0291_7";
                    }
                    try
                    {
                        var tt = JsonSerializer.Serialize<InboundPar>(PpckingPar);

                        request.Content = new StringContent(JsonSerializer.Serialize<InboundPar>(PpckingPar), Encoding.UTF8, "application/json");
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            //using var responseStream = await response.Content.ReadAsStreamAsync();
                            //var result = await JsonSerializer.DeserializeAsync
                            //    <string>(responseStream);
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return "调用接口失败1" + tt;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return "调用接口失败2" + e.Message.ToString();
                    }
                }
            }
        }

        public async Task<string> PickCreatFromUnpak(Machine_Entity machine, double Width, UnpackingAre_Entity unpackingAre, bool Flag, bool Flag2)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/Picking");
                    request.Headers.Add("Accept", "text / plain");

                    PickingPar PpckingPar = new PickingPar()
                    {
                        TASK_NO = DateTime.Now.ToString() + "PDA",
                        FROM_POSITION = unpackingAre.MESName.Split(",").First(),
                        TO_POSITION = machine.MESName.Split(",").First(),
                        TASK_SIZE = (Width / 1000).ToString(),

                        PICK_URGENT_YN = "0"
                    };
                    if (Flag)
                    {
                        PpckingPar.TASK_Diam = "11";
                    }
                    else
                    {
                        PpckingPar.TASK_Diam = "8";
                    }
                    if (Flag2)
                    {
                        PpckingPar.PICK_URGENT_YN = "1";

                    }
                    else
                    {
                        PpckingPar.PICK_URGENT_YN = "0";
                    }
                    try
                    {
                        request.Content = new StringContent(JsonSerializer.Serialize<PickingPar>(PpckingPar), Encoding.UTF8, "application/json");
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            //using var responseStream = await response.Content.ReadAsStreamAsync();
                            //var result = await JsonSerializer.DeserializeAsync
                            //    <string>(responseStream);
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return "调用接口失败";
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return "调用接口失败" + e.Message.ToString();
                    }
                }
            }
        }


        public async Task<string> PickCreatFromLK(Machine_Entity machine, double Width, bool Flag)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/Picking");
                    request.Headers.Add("Accept", "text / plain");

                    PickingPar PpckingPar = new PickingPar()
                    {

                        TASK_NO = DateTime.Now.ToString() + "PDA",
                        TASK_SIZE = ((float)(Width / 1000)).ToString(),
                        PICK_YN = "1",
                        TO_POSITION = machine.MESName.Split(",").First(),

                    };
                    if (Flag)
                    {
                        PpckingPar.TASK_Diam = "11";
                    }
                    else
                    {
                        PpckingPar.TASK_Diam = "7";
                    }
                    switch (machine.LkPostation)
                    {
                        case 1:
                            PpckingPar.FROM_POSITION = "K3TAGR101_1";
                            break;
                        case 2:
                            PpckingPar.FROM_POSITION = "K3TAGR101_2";
                            break;
                        case 3:
                            PpckingPar.FROM_POSITION = "K3TAGR101_3";
                            break;
                        case 4:
                            PpckingPar.FROM_POSITION = "K3TAGR101_4";
                            break;
                        default:
                            break;
                    }
                    if (Flag)
                    {
                        //PpckingPar.Type = 1;
                    }

                    try
                    {
                        request.Content = new StringContent(JsonSerializer.Serialize<PickingPar>(PpckingPar), Encoding.UTF8, "application/json");
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            //   using var responseStream = await response.Content.ReadAsStreamAsync();
                            //var result = await JsonSerializer.DeserializeAsync
                            //    <string>(responseStream);
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return "调用接口失败";
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return "调用接口失败" + e.Message.ToString();
                    }
                }
            }
        }

        public async Task<string> Feeding(Machine_Entity machine, string StarPoint, int type, string code, string lot)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/Feeding");
                    request.Headers.Add("Accept", "text / plain");

                    FeedingPar FeedingPar = new FeedingPar()
                    {

                        TO_POSITION = machine.MESName.Split(",").First(),
                        FROM_POSITION = machine.MESName.Split(",").First(),
                        TASK_CODE = code,
                        TASK_LOTID = lot,
                        TASK_NO = DateTime.Now.ToString() + "PDA",
                    };
                    if (type == 1)
                    {
                        FeedingPar.TASK_Diam = "11";
                    }
                    else
                    {
                        FeedingPar.TASK_Diam = "6";
                    }
                    try
                    {
                        request.Content = new StringContent(JsonSerializer.Serialize<FeedingPar>(FeedingPar), Encoding.UTF8, "application/json");
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            //   using var responseStream = await response.Content.ReadAsStreamAsync();
                            //var result = await JsonSerializer.DeserializeAsync
                            //    <string>(responseStream);
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return "调用接口失败1";
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return "调用接口失败2" + e.Message.ToString();
                    }
                }
            }
        }

        public async Task<string> HostreturnZJFromLK(Machine_Entity machine, double Width, bool Flag)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/Hostreturn");
                    request.Headers.Add("Accept", "text / plain");

                    Parreturn Parreturn = new Parreturn()
                    {
                        TASK_NO = DateTime.Now.ToString() + "PDA",
                        FROM_POSITION = machine.MESName.Split(",").First(),
                        TASK_WIDTH = (Width / 1000).ToString(),
                        PICK_YN = "1",
                        DOWN_YN = "1",
                    };
                    switch (machine.LkPostation)
                    {
                        case 1:
                            Parreturn.TO_POSITION = "K3TAGR101_1";
                            break;
                        case 2:
                            Parreturn.TO_POSITION = "K3TAGR101_2";
                            break;
                        case 3:
                            Parreturn.TO_POSITION = "K3TAGR101_3";
                            break;
                        case 4:
                            Parreturn.TO_POSITION = "K3TAGR101_4";
                            break;
                        default:
                            break;
                    }
                    if (Flag)
                    {
                        Parreturn.TASK_Diam = "11";
                    }
                    else
                    {
                        Parreturn.TASK_Diam = "6";
                    }
                    try
                    {
                        request.Content = new StringContent(JsonSerializer.Serialize<Parreturn>(Parreturn), Encoding.UTF8, "application/json");
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            //   using var responseStream = await response.Content.ReadAsStreamAsync();
                            //var result = await JsonSerializer.DeserializeAsync
                            //    <string>(responseStream);
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return "调用接口失败";
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return "调用接口失败";
                    }
                }
            }
        }

        public async Task<string> HostreturnZJUnpak(Machine_Entity machine, double Width, UnpackingAre_Entity unpackingAre_Entity, bool Flag, bool Flagempty)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/Hostreturn");
                    request.Headers.Add("Accept", "text / plain");

                    Parreturn Parreturn = new Parreturn()
                    {
                        TASK_NO = DateTime.Now.ToString() + "PDA",
                        FROM_POSITION = machine.MESName.Split(",").First(),
                        TO_POSITION = unpackingAre_Entity.MESName.Split(",").First(),
                        TASK_WIDTH = Width.ToString(),
                        TASK_SIZE = ((float)(Width / 1000)).ToString(),
                        PICK_YN = "0",
                        DOWN_YN = "1",
                    };
                    if (Flag)
                    {
                        Parreturn.TASK_Diam = "11";
                    }
                    else
                    {
                        Parreturn.TASK_Diam = "8";
                    }
                    if (Flagempty)
                    {
                        Parreturn.MATERIAL_TYPE = "空卷心";
                    }
                    try
                    {
                        request.Content = new StringContent(JsonSerializer.Serialize<Parreturn>(Parreturn), Encoding.UTF8, "application/json");
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            //  using var responseStream = await response.Content.ReadAsStreamAsync();
                            //var result = await JsonSerializer.DeserializeAsync
                            //    <string>(responseStream);
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return "调用接口失败";
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return "调用接口失败";
                    }
                }
            }
        }

        public async Task<string> HostreturnAGVFromLK(AGV_Entity aGV_Entity)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/Hostreturn");
                    request.Headers.Add("Accept", "text / plain");

                    Parreturn Parreturn = new Parreturn()
                    {
                        TASK_NO = DateTime.Now.ToString(),
                        FROM_POSITION = aGV_Entity.MESName,
                        // EndPoint = machine.Devid,
                        TASK_WIDTH = (aGV_Entity.HYWidth / 1000).ToString(),
                        PICK_YN = "1",
                        //  IsDown = true,

                    };
                    if (aGV_Entity.Type == 1)
                    {
                        Parreturn.TASK_Diam = "11";
                    }
                    else
                    {
                        Parreturn.TASK_Diam = "8";
                    }
                    switch (aGV_Entity.SourPostion)
                    {
                        case 1:
                            Parreturn.TO_POSITION = "K3TAGR101_1";
                            break;
                        case 2:
                            Parreturn.TO_POSITION = "K3TAGR101_2";
                            break;
                        case 3:
                            Parreturn.TO_POSITION = "K3TAGR101_3";
                            break;
                        case 4:
                            Parreturn.TO_POSITION = "K3TAGR101_4";
                            break;
                        default:
                            break;
                    }

                    try
                    {
                        request.Content = new StringContent(JsonSerializer.Serialize<Parreturn>(Parreturn), Encoding.UTF8, "application/json");
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            //  using var responseStream = await response.Content.ReadAsStreamAsync();
                            //var result = await JsonSerializer.DeserializeAsync
                            //    <string>(responseStream);
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return "调用接口失败";
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return "调用接口失败";
                    }
                }
            }
        }

        public async Task<string> HostreturnagvUnpak(AGV_Entity aGV_Entity, UnpackingAre_Entity unpackingAre_Entity, string formname)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/Hostreturn");
                    request.Headers.Add("Accept", "text / plain");

                    Parreturn Parreturn = new Parreturn()
                    {
                        TASK_NO = DateTime.Now.ToString() + "PDA",
                        FROM_POSITION = formname,
                        TO_POSITION = unpackingAre_Entity.MESName.Split(",").First(),
                        TASK_WIDTH = (aGV_Entity.HYWidth / 1000).ToString(),
                        DOWN_YN = "0",
                        PICK_YN = "0",
                        TASK_CODE = aGV_Entity.Code,
                        TASK_LOTID = aGV_Entity.Lot,
                        //  IsDown = true,
                        //  Postion = aGV_Entity.SourPostion,
                        //  Type = aGV_Entity.Type
                    };
                    if (aGV_Entity.Type == 1)
                    {
                        Parreturn.TASK_Diam = "11";
                    }
                    else
                    {
                        Parreturn.TASK_Diam = "8";
                    }
                    try
                    {
                        request.Content = new StringContent(JsonSerializer.Serialize<Parreturn>(Parreturn), Encoding.UTF8, "application/json");
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            // using var responseStream = await response.Content.ReadAsStreamAsync();
                            //var result = await JsonSerializer.DeserializeAsync
                            //    <string>(responseStream);
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return "调用接口失败";
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return "调用接口失败";
                    }
                }
            }
        }


        public async Task<string> HostreturnCacheFromUnpack(FullCache_Entity FullCache, UnpackingAre_Entity unpackingAre_Entity, string formname)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/Hostreturn");
                    request.Headers.Add("Accept", "text / plain");

                    Parreturn Parreturn = new Parreturn()
                    {
                        TASK_NO = DateTime.Now.ToString() + "PDA",
                        FROM_POSITION = formname.Split(",").First(),
                        TO_POSITION = unpackingAre_Entity.MESName,
                        TASK_WIDTH = (FullCache.Width / 1000).ToString(),
                        PICK_YN = "0",
                        DOWN_YN = "0",
                        TASK_CODE = FullCache.Code,
                        TASK_LOTID = FullCache.Lot,
                        //  IsDown = true,
                        //  Postion = aGV_Entity.SourPostion,
                        //   Type = FullCache.Type
                    };
                    if (FullCache.Type == 1)
                    {
                        Parreturn.TASK_Diam = "11";
                    }
                    else
                    {
                        Parreturn.TASK_Diam = "8";
                    }
                    try
                    {
                        request.Content = new StringContent(JsonSerializer.Serialize<Parreturn>(Parreturn), Encoding.UTF8, "application/json");
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            // using var responseStream = await response.Content.ReadAsStreamAsync();
                            //var result = await JsonSerializer.DeserializeAsync
                            //    <string>(responseStream);
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return "调用接口失败";
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return "调用接口失败";
                    }
                }
            }
        }


    }
}
